<template>
  <el-dialog :model-value="props.show" title="分配角色" @close="emit('hide')">
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name" :label-width="formLabelWidth">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="请输入角色名称"
        />
      </el-form-item>
      <el-form-item label="描述" :label-width="formLabelWidth">
        <el-input
          v-model="form.description"
          :rows="4"
          type="textarea"
          autocomplete="off"
          placeholder="请输入描述"
        />
      </el-form-item>
      <el-form-item label="是否启用" :label-width="formLabelWidth">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="$emit('hide')">取消</el-button>
        <el-button type="primary" @click="submitFn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref, watch } from "vue";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { getRoleListApi, roleCreateApi, roleUpdateApi } from "./api";
const formLabelWidth = "140px";
const props = defineProps({
  show: { type: Boolean, default: false },
  id: {
    type: Number,
    default: 0,
  },
});
watch(
  () => props.id,
  () => {
    if (props.id !== 0) {
      getRoleListApi().then((res) => {
        if (res.code === 200) {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].id == props.id) {
              Object.assign(form, res.data[i]);
            }
          }
        }
      });
    }
  }
);

const emit = defineEmits(["hide"]);
const form = reactive<IRoleModel>({
  description: "",
  name: "",
  status: 1,
  id: 0,
});
const formRef = ref<FormInstance>();
const rules = {
  name: [{ required: true, trigger: "blur", message: "请填写用户名" }],
};
const addUser = () => {
  ElMessageBox.confirm("确定要添加用户吗？", "添加用户", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      roleCreateApi(form).then((res) => {
        if (res.code === 200) {
          ElMessage.success("添加成功");
          emit("hide", true);
          formRef.value?.resetFields();
        }
      });
    })
    .catch(() => {
      console.log("点击的取消");
    });
};
const editUser = () => {
  ElMessageBox.confirm("确定要修改吗？", "修改用户信息", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      form.id = props.id;
      roleUpdateApi(form).then((res) => {
        if (res.code === 200) {
          ElMessage.success("修改成功");
          formRef.value?.resetFields();
          emit("hide", true);
        }
      });
    })
    .catch(() => {});
};
const submitFn = () => {
  formRef.value?.validate((ok: boolean) => {
    if (ok) {
      if (props.id === 0) {
        addUser();
      } else {
        editUser();
      }
    }
  });
};
</script>
<style lang="less" scoped></style>
